package com.yuanrui.wechat.controller;

import com.yuanrui.wechat.handler.WechatMsgFactory;
import com.yuanrui.wechat.handler.WechatMsgHandler;
import com.yuanrui.wechat.utils.MessageUtil;
import com.yuanrui.wechat.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
@RequestMapping("/wechat/")
public class CallBackController {
    private static final String TOKEN = "yuanrui";

    @Resource
    private WechatMsgFactory wechatMsgFactory;

    /**
     * 回调消息校验
     *
     * @return 校验结果
     */
    @GetMapping("callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数：signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr);
        //对参数进行字典排序
        String shaStr = SHA1.getSHA1(TOKEN, timestamp, nonce, "");
        if (shaStr.equals(signature)) {
            return echostr;
        } else {
            return "unknown";
        }
    }

    /**
     * 接收消息
     *
     * @return 消息
     */
    @PostMapping(value = "callback", produces = "application/xml;charset=UTF-8")
    public String callback(@RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature", required = false) String msgSignature) {
        log.info("接收到微信请求：signature:{},timestamp:{},nonce:{},msgSignature:{},requestBody:{}", signature, timestamp, nonce, msgSignature, requestBody);
        Map<String, String> map = MessageUtil.parseXml(requestBody);
        String msgType = map.get("MsgType");
        String event = map.get("Event") == null ? "" : map.get("Event");
        log.info("msgType:{},event:{}", msgType, event);
        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if (StringUtils.isNotBlank(event)) {
            sb.append(".").append(event);
        }
        WechatMsgHandler wechatMsgHandler = wechatMsgFactory.getWechatMsgHandler(sb.toString());
        if (Objects.isNull(wechatMsgHandler)) {
            return "unknown";
        }
        return wechatMsgHandler.dealMsg(map);
//        String msg="<xml>\n" +
//                "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
//                "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//                "  <CreateTime>12345678</CreateTime>\n" +
//                "  <MsgType><![CDATA[text]]></MsgType>\n" +
//                "  <Content><![CDATA[你好,欢迎关注]]></Content>\n" +
//                "</xml>";
    }
}
